﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DataTransfer;
namespace Gui
{
    public partial class frmThemNhanVien : DevComponents.DotNetBar.Office2007Form
    {
        UserBus userBus;
        frmDanhSachNhanVien frm_DSNV;
        public frmThemNhanVien(frmDanhSachNhanVien frm,User user=null )
        {
            InitializeComponent();
            frm_DSNV = frm;
            if (user != null)
            {
                btnThemNhanVien.Visible = false;
                txtUserName.ReadOnly = true;
                txtUserName.Text = user.UserName;
                txtTenNhanVien.Text = user.HoTen;
                txtPass.Text = user.Pass;
                txtRePass.Text = user.Pass;
                dtpNgaySinh.Value =(DateTime) user.NgaySinh;
                rbNam.Checked = user.GioiTinh == 1 ? true : rbNu.Checked = true;
                ckAct.Checked = user.act == 1 ? true : false;
            }
            else
            {

                btnCapNhat.Visible = false;
            }
        }

        private void frmThemNhanVien_Load(object sender, EventArgs e)
        {

        }

        

        

        private void btnThemNhanVien_Click(object sender, EventArgs e)
        {
            if(checkForm())
            {
            User info = new User();
            info.HoTen = txtTenNhanVien.Text;
            info.UserName = txtUserName.Text;
            info.Pass = txtUserName.Text;
            info.GioiTinh = rbNam.Checked == true ? 1 : 0;
            info.NgaySinh = dtpNgaySinh.Value.Date;
            info.act = ckAct.Checked==true?1:0;
            userBus = new UserBus();
            userBus.Insert(info);
            MessageBox.Show("Insert Success ");
            }
         }
        private bool checkForm()
        {
           string error = "";
            if (string.IsNullOrEmpty(txtUserName.Text))
                error += "UserName mot NuLL__";
            if (string.IsNullOrEmpty(txtPass.Text))
                error += "Pass not Null__";
            if (txtPass.Text != txtRePass.Text)
                error += "RePass not Same Pass__";
            if (error != "")
            {
                MessageBox.Show(error);
                return false;
            }
            return true;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
             if(checkForm())
            {
            User info = new User();
              
            info.HoTen = txtTenNhanVien.Text;
            info.UserName = txtUserName.Text;
            info.Pass = txtUserName.Text;
            info.GioiTinh = rbNam.Checked == true ? 1 : 0;
            info.NgaySinh = dtpNgaySinh.Value.Date;
            info.act = ckAct.Checked == true ? 1 : 0;
            userBus = new UserBus();
            userBus.Update(info);
            MessageBox.Show("Update Success ");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThemNhanVien_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm_DSNV.load();
        }
    }
}
